/*
 * =====================================================================================
 *
 *       Filename:  his_mqtt_topic.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  11/08/2019 10:42:35 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  lipeng, lipeng@hisome.com
 *        Company:  HISOME
 *      Copyright:  Copyright (c) 2019-HISOME
 *
 * =====================================================================================
 */
#ifndef __HIS_MQTT_TOPIC_H__
#define __HIS_MQTT_TOPIC_H__
	
#ifdef __cplusplus
	extern "C" {
#endif
	
#ifdef __linux__
#include <errno.h>
#endif/*__linux__*/

#include <stddef.h>

typedef struct _topic_struct {
	char *topic;
	char *topicACK;
}topic_struct;

#define  HS_MQTT_MSG_MODULE_CORE   		"CORE"
#define  HS_MQTT_MSG_MODULE_SYSMGT 		"SYSMGT"
#define  HS_MQTT_MSG_MODULE_AV 			"AV"
#define  HS_MQTT_MSG_MODULE_SIPUA 		"SIPUA"
#define  HS_MQTT_MSG_MODULE_SIPSERVER 	"SIPSERVER"
#define  HS_MQTT_MSG_MODULE_ENCRYPT 	"ENCRYPT"
#define  HS_MQTT_MSG_MODULE_ALARM 		"ALARM"
#define  HS_MQTT_MSG_MODULE_MGTPLATFORM "MGTPLATFORM"
#define  HS_MQTT_MSG_MODULE_UNKNOWN 	"UNKNOWN"
 
enum HS_MQTT_MSG_MODULE_ID{
 HS_MQTT_MSG_MODULE_ID_CORE = 0,
 HS_MQTT_MSG_MODULE_ID_SYSMGT, //1
 HS_MQTT_MSG_MODULE_ID_AV, //2
 HS_MQTT_MSG_MODULE_ID_SIPUA, //3
 HS_MQTT_MSG_MODULE_ID_SIPSERVER, //4
 HS_MQTT_MSG_MODULE_ID_ENCRYPT, //5
 HS_MQTT_MSG_MODULE_ID_ALARM, //6
 HS_MQTT_MSG_MODULE_ID_MGTPLATFORM,//7
 HS_MQTT_MSG_MODULE_ID_MAX, //8
};

extern int msgCount[HS_MQTT_MSG_MODULE_ID_MAX+1];

//topic:  DEV/SYSMGT/xxxxxx    or  DEV_127.0.0.1/SYSMGT/xxxxxx    
#define hs_mqtt_topic_create(topicAll, topicLen, IP, modId, fmt, args...)  \
		do{ \
			char *HS_MQTT_TOPIC_LEVEL_1 = NULL;\
			if(modId == HS_MQTT_MSG_MODULE_ID_CORE){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_CORE;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_SYSMGT){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_SYSMGT;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_AV){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_AV;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_SIPUA){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_SIPUA;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_SIPSERVER){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_SIPSERVER;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_ENCRYPT){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_ENCRYPT;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_ALARM){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_ALARM;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_MGTPLATFORM){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_MGTPLATFORM;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_MAX){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_UNKNOWN;}\	
			if(topicAll == NULL){ \
				break; \
			}else{ \
				if(IP == NULL){\	
					snprintf(topicAll, topicLen-1, "DEV/%s/" fmt, HS_MQTT_TOPIC_LEVEL_1, ##args); \
				}else{\
					snprintf(topicAll, topicLen-1, "DEV_%s/%s/" fmt, IP, HS_MQTT_TOPIC_LEVEL_1, ##args); \
				}\		
			} \	
		}while(0);
#define hs_mqtt_topicAck_create(topicAll, topicLen, IP, modId, fmt, args...)  \
		do{ \
			char *HS_MQTT_TOPIC_LEVEL_1 = NULL;\
			if(modId == HS_MQTT_MSG_MODULE_ID_CORE){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_CORE;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_SYSMGT){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_SYSMGT;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_AV){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_AV;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_SIPUA){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_SIPUA;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_SIPSERVER){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_SIPSERVER;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_ENCRYPT){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_ENCRYPT;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_ALARM){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_ALARM;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_MGTPLATFORM){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_MGTPLATFORM;}\
			else if(modId == HS_MQTT_MSG_MODULE_ID_MAX){HS_MQTT_TOPIC_LEVEL_1 = HS_MQTT_MSG_MODULE_UNKNOWN;}\	
			if(topicAll == NULL){ \
				break; \
			}else{ \
				if(IP == NULL){\	
					snprintf(topicAll, topicLen-1, "DEV/%s/"fmt"/ACK", HS_MQTT_TOPIC_LEVEL_1, ##args); \
				}else{\
					snprintf(topicAll, topicLen-1, "DEV_%s/%s/"fmt"/ACK", IP, HS_MQTT_TOPIC_LEVEL_1, ##args); \
				}\		
			} \	
		}while(0);
#define hs_mqtt_msgid_update(moduleId, msgId) \
		do{\
			if(msgId == NULL)\
				break;\
			if(moduleId < HS_MQTT_MSG_MODULE_ID_CORE && moduleId >= HS_MQTT_MSG_MODULE_ID_MAX){\
				break;\	
			}\
			*msgId = ((moduleId<<24)&0xff000000)|(msgCount[moduleId]&0xffffff);\
			 msgCount[moduleId]	++;	\
		}while(0);	
/*************************************************************************
example:
	char topic[128] = {0};
	int messageId = 100;
	static int msgCount[HS_MQTT_MSG_MODULE_ID_MAX+1] = {0};
	hs_mqtt_topic_create(topic, 128, NULL, HS_MQTT_MSG_MODULE_ID_SYSMGT, "abcdefg%d", messageId);	
	hs_mqtt_msgid_update(HS_MQTT_MSG_MODULE_ID_AV, &messageId);
	hs_mqtt_msgid_update(HS_MQTT_MSG_MODULE_ID_AV, &messageId);
	hs_mqtt_msgid_update(HS_MQTT_MSG_MODULE_ID_AV, &messageId);
	printf("topic: %s, msgId:0x%08x\n", topic, messageId);	
	hs_mqtt_topicAck_create(topic, 128, "127.0.0.1", HS_MQTT_MSG_MODULE_ID_SYSMGT, "abcdefg");	
	hs_mqtt_msgid_update(HS_MQTT_MSG_MODULE_ID_AV, &messageId);
	hs_mqtt_msgid_update(HS_MQTT_MSG_MODULE_ID_AV, &messageId);
	hs_mqtt_msgid_update(HS_MQTT_MSG_MODULE_ID_AV, &messageId);
	printf("topic: %s, msgId:0x%08x\n", topic, messageId);	
Console info:	
	topic: DEV/SYSMGT/abcdefg100, msgId:0x02000002
	topic: DEV_127.0.0.1/SYSMGT/abcdefg/ACK, msgId:0x02000005  	 	
**************************************************************************/
#ifdef __cplusplus
};
#endif

#endif /* __HIS_MQTT_TOPIC_H__ */


